package com.maigu.controller;

import com.maigu.dataobject.ProductCategory;
import com.maigu.dataobject.ProductInfo;
import com.maigu.dto.OrderDTO;
import com.maigu.exception.SellException;
import com.maigu.form.ProductForm;
import com.maigu.service.CategoryService;
import com.maigu.service.OrderService;
import com.maigu.service.ProductService;
import com.maigu.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 商品列表
     * @param page
     * @param size
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "3") Integer size,
                             Map<String,Object> map)
    {
        PageRequest request=new PageRequest(page-1,size);
        Page<ProductInfo> productInfoPage = productService.findAll(request);
        map.put("productInfoPage",productInfoPage);
        map.put("currentPage",page);
        map.put("size",3);
        return  new ModelAndView("product/list",map);
    }
    /**
     * 商品上架
     * @param
     * @param map
     * @return
     */
    @GetMapping("/onsale")
    public ModelAndView onsale(@RequestParam("productId") String productId,Map<String,Object> map)
    {

        try {
            productService.onSale(productId);
        }
        catch (SellException e)
        {
            log.error("[商品状态]发生异常{}",e);
            map.put("msg", e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg","");
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }
    /**
     * 商品下架
     * @param
     * @param map
     * @return
     */
    @GetMapping("/offsale")
    public ModelAndView offsale(@RequestParam("productId") String productId,Map<String,Object> map)
    {

        try {
            productService.offSale(productId);
        }
        catch (SellException e)
        {
            log.error("[商品状态]发生异常{}",e);
            map.put("msg", e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("msg","");
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId",required = false) String productId,Map<String,Object> map)
    {
            if(!StringUtils.isEmpty(productId))
            {
                ProductInfo productInfo = productService.finOne(productId);
                map.put("productInfo",productInfo);
            }
            //商品类别
            List<ProductCategory> productCategoryList = categoryService.finAll();
            map.put("productCategoryList",productCategoryList);
            return new ModelAndView("product/index",map);


        }
        @PostMapping("/save")
     public ModelAndView save(@Valid ProductForm productForm, BindingResult bindingResult, Map<String, Object> map)
        {
            if(bindingResult.hasErrors())
            {
                map.put("msg",bindingResult.getFieldError().getDefaultMessage());
                map.put("url","/sell/seller/product/index");
                return new ModelAndView("common/error",map);
            }
            ProductInfo productInfo=new ProductInfo();
            try {
                 //如果商品ID不为空状态为修改
                if(!StringUtils.isEmpty(productForm.getProductId())) {
                    productInfo = productService.finOne(productForm.getProductId());
                }
                else
                {
                    productForm.setProductId(KeyUtil.genUniqueKey());
                }
                BeanUtils.copyProperties(productForm,productInfo);
                productService.save(productInfo);
            }
           catch (SellException e)
           {
               map.put("msg",e.getMessage());
               map.put("url","/sell/seller/product/index");
               return new ModelAndView("common/error",map);
           }
            map.put("msg","保存成功！");
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/success",map);

        }





}
